package com.xyht.medicine_management_system.modules.addresses.controller;


import com.xyht.medicine_management_system.auth.util.JwtTokenUtil;
import com.xyht.medicine_management_system.common.annotation.RequestPass;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.medicine_management_system.modules.addresses.service.MmsAddressesService;
import com.xyht.medicine_management_system.modules.addresses.entity.req.MmsAddressesReq;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * <p>
 * 地址表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-12-03
 */
@RestController
@SecretBody
@RequestMapping("/address")
public class MmsAddressesController {
    @Resource
    private MmsAddressesService mmsAddressesService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty("添加地址")
    @PostMapping("/add")
    @RequestPass
    public ResponseResult addAddresses(@RequestBody MmsAddressesReq addressesReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsAddressesService.addAddresses(addressesReq, userId);
    }

    @ApiModelProperty("删除地址")
    @PostMapping("/del")
    @RequestPass
    public ResponseResult delAddresses(@RequestParam("addressId") String[] addressId) {
        mmsAddressesService.removeByIds(Arrays.asList(addressId));
        return CommonResult.success();
    }

    @ApiModelProperty("修改地址")
    @PostMapping("/update")
    @RequestPass
    public ResponseResult updateAddresses(@RequestBody MmsAddressesReq addressesReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsAddressesService.updateAddresses(addressesReq, userId);
    }

    @ApiModelProperty("地址列表")
    @GetMapping("/list")
    public ResponseResult listAddresses(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                        @RequestParam(value = "searchStr", required = false) String searchStr,
                                        HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsAddressesService.listAddresses(pageNum, pageSize, searchStr, userId);
    }

    @ApiModelProperty("地址详情")
    @GetMapping("/detail")
    public ResponseResult addressesDetail(@RequestParam("addressId") String addressId) {
        return mmsAddressesService.addressesDetail(addressId);
    }

}

